package com.example.script.admin.config;

import com.example.script.admin.service.AdminUserService;
import com.example.script.model.AdminUser;
import com.example.script.model.SysUser;
import com.example.script.security.config.SecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;


@EnableWebSecurity
@Configuration
public class AdminSecurityConfig extends SecurityConfig {
    @Autowired
    private AdminUserService adminUserService;
    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return new UserDetailsService() {//实现了个匿名内部类（直接实现）
            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                AdminUser adminUser = adminUserService.getAdminUserByUsername(username);
                if (adminUser !=null){
                    return new com.example.script.admin.model.AdminSecurityConfig(adminUser);
                }
                return null;
            }
        };
    }

}
